package com.lanhe.member.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanhe.member.domain.LhStatDailyIncome;
import com.lanhe.member.mapper.LhStatDailyIncomeMapper;
import com.lanhe.member.service.ILhStatDailyIncomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * (LhStatDailyIncome)表服务实现类
 *
 * @author zhanggl
 * @since 2023-12-11 09:58:47
 */
@Service
public class LhStatDailyIncomeServiceImpl extends ServiceImpl<LhStatDailyIncomeMapper, LhStatDailyIncome> implements ILhStatDailyIncomeService {

	@Autowired
	private LhStatDailyIncomeMapper lhStatDailyIncomeMapper;

	/**
	 * 新增
	 *
	 * @param statDailyIncome 实体
	 * @return int
	 * @author zhanggl
	 * @date 2023/12/11
	 */
	@Override
	public int insertStatDailyIncome(LhStatDailyIncome statDailyIncome) {

		LambdaQueryWrapper<LhStatDailyIncome> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(LhStatDailyIncome::getMemberId, statDailyIncome.getMemberId());
		lambdaQueryWrapper.eq(LhStatDailyIncome::getYear, statDailyIncome.getYear());
		lambdaQueryWrapper.eq(LhStatDailyIncome::getMonthly, statDailyIncome.getMonthly());
		lambdaQueryWrapper.eq(LhStatDailyIncome::getDaily, statDailyIncome.getDaily());
		LhStatDailyIncome statStatDaily = lhStatDailyIncomeMapper.selectOne(lambdaQueryWrapper);
		if (ObjectUtil.isNotNull(statStatDaily)) {

			statDailyIncome.setUpdateTime(new Date());
			statDailyIncome.setAmount(statStatDaily.getAmount().add(statDailyIncome.getAmount()));
			return lhStatDailyIncomeMapper.update(statDailyIncome,lambdaQueryWrapper);
		} else {

			statDailyIncome.setCreateTime(new Date());
			return lhStatDailyIncomeMapper.insert(statDailyIncome);
		}
	}

	@Override
	public List<LhStatDailyIncome> rankingList(Integer pageNum, Integer pageSize) {

		// 获取当前日期
		LocalDate currentDate = LocalDate.now();

		LambdaQueryWrapper<LhStatDailyIncome> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(LhStatDailyIncome::getYear,currentDate.getYear());
		lambdaQueryWrapper.eq(LhStatDailyIncome::getMonthly,currentDate.getYear()+""+currentDate.getMonthValue());
		lambdaQueryWrapper.eq(LhStatDailyIncome::getDaily,Integer.valueOf(currentDate.getYear()+""+currentDate.getMonthValue()+""+currentDate.getDayOfMonth()));
		lambdaQueryWrapper.orderByDesc(LhStatDailyIncome::getAmount);
		return lhStatDailyIncomeMapper.selectList(lambdaQueryWrapper);
	}

	@Override
	public LhStatDailyIncome getInfo(LhStatDailyIncome statDailyIncome) {
		LambdaQueryWrapper<LhStatDailyIncome> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(LhStatDailyIncome::getMemberId, statDailyIncome.getMemberId());
		lambdaQueryWrapper.eq(LhStatDailyIncome::getYear, statDailyIncome.getYear());
		lambdaQueryWrapper.eq(LhStatDailyIncome::getMonthly, statDailyIncome.getMonthly());
		lambdaQueryWrapper.eq(LhStatDailyIncome::getDaily, statDailyIncome.getDaily());
		return lhStatDailyIncomeMapper.selectOne(lambdaQueryWrapper);
	}
}

